package com.rem.hutool.my2Convert;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.CharsetUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 类型转换
 *
 * @author Rem
 * @date 2020-08-18
 */

public class ConvertDemo {

    public static void main(String[] args) {


        //toCharset();

        //toList();

        //toDate();

        //toArray();

        //toStr();
    }

    /**
     * 编码转换
     */
    private static void toCharset() {
        String a = "我不是乱码";
        //转换后result为乱码
        String result = Convert.convertCharset(a, CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1);
        String raw = Convert.convertCharset(result, CharsetUtil.ISO_8859_1, "UTF-8");
        System.err.println(a.equals(raw));
    }


    /**
     * 转换为集合
     */
    private static void toList() {
        Object[] a = {"a", "你", "好", "", 1};
        List list = Convert.toList(a);
        System.err.println(list.toString());
    }

    /**
     * 转换为日期对象
     */
    private static void toDate() {
        String a = "2020-08-08";
        Date value = Convert.toDate(a);
        System.err.println(value);
        Date date = Convert.toDate(System.currentTimeMillis());
        System.err.println(date);
    }

    /**
     * 转换为指定类型数组
     */
    private static void toArray() {
        String[] b = {"1", "2", "3", "4"};
        //结果为Integer数组
        Integer[] intArray = Convert.toIntArray(b);
        System.err.println(Arrays.toString(intArray));
        long[] c = {1, 2, 3, 4, 5};
        //结果为Integer数组
        Integer[] intArray2 = Convert.toIntArray(c);
        System.err.println(Arrays.toString(intArray2));

        String[] strArray3 = Convert.toStrArray(c);
        System.err.println(Arrays.toString(strArray3));
    }

    /**
     * 转换为字符串
     */
    private static void toStr() {
        int a = 1;
        //aStr为"1"
        String aStr = Convert.toStr(a);
        System.err.println(aStr);

        long[] b = {1, 2, 3, 4, 5};
        //bStr为："[1, 2, 3, 4, 5]"
        String bStr = Convert.toStr(b);
        System.err.println(bStr);
    }


}
